package com.healthcloud.service.RegionalDisease.impl;


import com.healthcloud.dao.RegionalDisease.TestprojectitemmonitorDao;
import com.healthcloud.entity.*;
import com.healthcloud.entity.RegionalDisease.SlowDiseaseStatisticsEntity;
import com.healthcloud.entity.RegionalDisease.SlowDiseaseStatisticsVo;
import com.healthcloud.entity.RegionalDisease.TestprojectitemmonitorEntity;
import com.healthcloud.service.RegionalDisease.AreaGroupService;
import com.healthcloud.service.TestprojectitemService;
import com.healthcloud.service.RegionalDisease.TestprojectitemmonitorService;
import com.healthcloud.utils.IdcardInfoExtractorUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;


@Service("testprojectitemmonitorService")
@Transactional
public class TestprojectitemmonitorServiceImpl implements TestprojectitemmonitorService {

    @Autowired
    private TestprojectitemmonitorDao testprojectitemmonitorDao;

    @Autowired
    private AreaGroupService areaGroupService;

    @Autowired
    private TestprojectitemService testprojectitemService;

    public List<SlowDiseaseStatisticsEntity> showTestItemMonitor(String testKindCode, String year, Long areaCode, String level) {
        int allCount = 0, manCount = 0, womenCount = 0, upperCount = 0, lowerCount = 0;        //人数
        int totalTime, upperTime, lowerTime;                                            //次数
        int age1 = 0, age2 = 0, age3 = 0, age4 = 0, age5 = 0, age6 = 0, age7 = 0;        //年龄段人数
        int xyallCount = 0, xymanCount = 0, xywomenCount = 0, xyupperCount = 0, xylowerCount = 0;        //人数
        int xytotalTime = 0, xyupperTime = 0, xylowerTime = 0;
        int xyage1 = 0, xyage2 = 0, xyage3 = 0, xyage4 = 0, xyage5 = 0, xyage6 = 0, xyage7 = 0;        //年龄段人数(血压)
        boolean flag = true;
        List<SlowDiseaseStatisticsEntity> slowDiseaseStatisticsEntities = new ArrayList<>();
        List<SlowDiseaseStatisticsEntity> list = new ArrayList<>();

        //根据区域和层级获取该区域内的医院
        List<HlHospitalEntity> hospitalList = areaGroupService.queryByAreaCode(areaCode, level);
        List<Long> list1 = new ArrayList<>();
        for (HlHospitalEntity hlHospitalEntity : hospitalList) {
            list1.add(hlHospitalEntity.getId());
        }

        //根据大类编码查询出小类
        List<TestprojectitemEntity> testprojectitemEntityList = testprojectitemService.queryListMsg(testKindCode);
        for (TestprojectitemEntity testprojectitemEntity : testprojectitemEntityList) {
            SlowDiseaseStatisticsEntity slowDiseaseStatisticsEntity = new SlowDiseaseStatisticsEntity();
//            //测试人数固定字段
//            slowDiseaseStatisticsEntity.setPersonTotalTitle("测试");
//            slowDiseaseStatisticsEntity.setPersonLowerTitle("偏低");
//            slowDiseaseStatisticsEntity.setPersonUpperTitle("偏高");
//            //测试次数固定字段
//            slowDiseaseStatisticsEntity.setTestTotalTitle("测试");
//            slowDiseaseStatisticsEntity.setTestLowerTitle("偏低");
//            slowDiseaseStatisticsEntity.setTestUpperTitle("偏高");
            manCount = womenCount = lowerCount = upperCount = 0;
            totalTime = upperTime = lowerTime =  0;
            age1 = age2 = age3 = age4 = age5 = age6 = age7 = 0;

            List<TestprojectitemmonitorEntity> Msg = new ArrayList<>();
//            allCount = 0;
            if (list1 != null && !list1.isEmpty()) {
                Msg = testprojectitemmonitorDao.queryMsgByTestItemCode(
                        testprojectitemEntity.getTestItemCode(), year, list1);
                allCount = testprojectitemmonitorDao.queryCount(testprojectitemEntity.getTestItemCode(), year, list1);//测试人数
            }

            for (TestprojectitemmonitorEntity testprojectitemmonitorEntity : Msg) {
                if (testprojectitemmonitorEntity.getType() != null) {
                    if (1 == testprojectitemmonitorEntity.getType()) {
                        upperCount++;        //偏高人数
                    } else if (2 == testprojectitemmonitorEntity.getType()) {
                        lowerCount++;        //偏低人数
                    }
                }
                totalTime += testprojectitemmonitorEntity.getTotalCount();         //测试次数
                upperTime += testprojectitemmonitorEntity.getUpperCount();        //偏高次数
                lowerTime += testprojectitemmonitorEntity.getLowerCount();        //偏低次数
                IdcardInfoExtractorUtil id = new IdcardInfoExtractorUtil(testprojectitemmonitorEntity.getIdcard());

                if ("舒张压".equals(testprojectitemEntity.getTestItemName()) || "收缩压".equals(
                        testprojectitemEntity.getTestItemName())) {
                    if ("0".equals(id.getGender()) || "1".equals(id.getGender())) {
                        xymanCount++;
                    } else if ("2".equals(id.getGender())) {
                        xywomenCount++;
                    }
                    if (id.getAge() >= 0 && id.getAge() < 20) {
                        xyage1++;
                    } else if (id.getAge() >= 30 && id.getAge() < 40) {
                        xyage3++;
                    } else if (id.getAge() >= 40 && id.getAge() < 50) {
                        xyage4++;
                    } else if (id.getAge() >= 50 && id.getAge() < 60) {
                        xyage5++;
                    } else if (id.getAge() >= 60 && id.getAge() < 70) {
                        xyage6++;
                    } else if (id.getAge() >= 20 && id.getAge() < 30) {
                        xyage2++;
                    } else {
                        xyage7++;
                    }
                } else {
                    if ("0".equals(id.getGender()) || "1".equals(id.getGender())) {
                        manCount++;
                    } else if ("2".equals(id.getGender())) {
                        womenCount++;
                    }
                    if (id.getAge() >= 0 && id.getAge() < 20) {
                        age1++;
                    } else if (id.getAge() >= 20 && id.getAge() < 30) {
                        age2++;
                    } else if (id.getAge() >= 30 && id.getAge() < 40) {
                        age3++;
                    } else if (id.getAge() >= 40 && id.getAge() < 50) {
                        age4++;
                    } else if (id.getAge() >= 50 && id.getAge() < 60) {
                        age5++;
                    } else if (id.getAge() >= 60 && id.getAge() < 70) {
                        age6++;
                    } else {
                        age7++;
                    }
                }
            }

            //总标题
            slowDiseaseStatisticsEntity.setPersonTitle(testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setTestTitle(testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setPercentTitle("异常" + testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setAgeDistributionTitle("异常" + testprojectitemEntity.getTestItemName());

            //测试人数相关数据
            slowDiseaseStatisticsEntity.setPersonTotal(String.valueOf(allCount));
            slowDiseaseStatisticsEntity.setPersonUpper(String.valueOf(upperCount));
            slowDiseaseStatisticsEntity.setPersonLower(String.valueOf(lowerCount));

            //测试次数相关数据
            slowDiseaseStatisticsEntity.setTestTotal(String.valueOf(totalTime));
            slowDiseaseStatisticsEntity.setTestUpper(String.valueOf(upperTime));
            slowDiseaseStatisticsEntity.setTestLower(String.valueOf(lowerTime));
            if ((manCount + womenCount) != 0) {
                //异常男女比例
                slowDiseaseStatisticsEntity.setManCount(manCount);
                slowDiseaseStatisticsEntity.setManPercent(
                        new BigDecimal((float) manCount / (manCount + womenCount) * 100).setScale(0,
                                BigDecimal.ROUND_HALF_UP) + "%");
                slowDiseaseStatisticsEntity.setWomanCount(womenCount);
                slowDiseaseStatisticsEntity.setWomanPercent(
                        new BigDecimal((float) womenCount / (manCount + womenCount) * 100).setScale(0,
                                BigDecimal.ROUND_HALF_UP) + "%");
                //异常年龄段分布
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                DecimalFormat df = new DecimalFormat("0.00");
                map.put("0-20岁", df.format((float) age1 / (manCount + womenCount) * 100) + "%");
                map.put("20-30岁", df.format((float) age2 / (manCount + womenCount) * 100) + "%");
                map.put("30-40岁", df.format((float) age3 / (manCount + womenCount) * 100) + "%");
                map.put("40-50岁", df.format((float) age4 / (manCount + womenCount) * 100) + "%");
                map.put("50-60岁", df.format((float) age5 / (manCount + womenCount) * 100) + "%");
                map.put("60-70岁", df.format((float) age6 / (manCount + womenCount) * 100) + "%");
                map.put("70岁以上", df.format((float) age7 / (manCount + womenCount) * 100) + "%");
                slowDiseaseStatisticsEntity.setAgeDistribution(map);
            } else {
                slowDiseaseStatisticsEntity.setManCount(0);
                slowDiseaseStatisticsEntity.setManPercent("0%");
                slowDiseaseStatisticsEntity.setWomanCount(0);
                slowDiseaseStatisticsEntity.setWomanPercent("0%");
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("0-20岁", "0.00%");
                map.put("20-30岁", "0.00%");
                map.put("30-40岁", "0.00%");
                map.put("40-50岁", "0.00%");
                map.put("50-60岁", "0.00%");
                map.put("60-70岁", "0.00%");
                map.put("70岁以上", "0.00%");
                slowDiseaseStatisticsEntity.setAgeDistribution(map);
            }
            if ("舒张压".equals(slowDiseaseStatisticsEntity.getTestTitle()) || "收缩压".equals(
                    slowDiseaseStatisticsEntity.getTestTitle())) {
                list.add(slowDiseaseStatisticsEntity);
            } else {
                slowDiseaseStatisticsEntities.add(slowDiseaseStatisticsEntity);
            }
            if (list.size() == 2 && flag == true) {
                SlowDiseaseStatisticsEntity entity = new SlowDiseaseStatisticsEntity();
                System.out.println("***********************************--------------------------------------------");
                entity.setTestTitle("血压");
                entity.setPersonTitle("血压");
                entity.setPercentTitle("异常血压");
                entity.setAgeDistributionTitle("异常血压");
                for (SlowDiseaseStatisticsEntity entities : list) {
                    xyallCount += Integer.parseInt(entities.getPersonTotal());
                    xyupperCount += Integer.parseInt(entities.getPersonUpper());
                    xylowerCount += Integer.parseInt(entities.getPersonLower());
                    xytotalTime += Integer.parseInt(entities.getTestTotal());
                    xyupperTime += Integer.parseInt(entities.getTestUpper());
                    xylowerTime += Integer.parseInt(entities.getTestLower());
                    //测试人数
                    entity.setPersonTotal(String.valueOf(xyallCount));
                    entity.setPersonUpper(String.valueOf(xyupperCount));
                    entity.setPersonLower(String.valueOf(xylowerCount));
                    //测试次数
                    entity.setTestTotal(String.valueOf(xytotalTime));
                    entity.setTestUpper(String.valueOf(xyupperTime));
                    entity.setTestLower(String.valueOf(xylowerTime));
                    if ((xywomenCount + xymanCount) != 0) {
                        entity.setWomanPercent(
                                new BigDecimal((float) xywomenCount / (xywomenCount + xymanCount) * 100).setScale(0,
                                        BigDecimal.ROUND_HALF_UP) + "%");
                        entity.setWomanCount(xywomenCount);
                        entity.setManPercent(
                                new BigDecimal((float) xymanCount / (xywomenCount + xymanCount) * 100).setScale(0,
                                        BigDecimal.ROUND_HALF_UP) + "%");
                        entity.setManCount(xymanCount);
                        //异常年龄段分布
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        DecimalFormat df = new DecimalFormat("0.00");
                        map.put("0-20岁", df.format((float) xyage1 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("20-30岁", df.format((float) xyage2 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("30-40岁", df.format((float) xyage3 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("40-50岁", df.format((float) xyage4 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("50-60岁", df.format((float) xyage5 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("60-70岁", df.format((float) xyage6 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("70岁以上", df.format((float) xyage7 / (xywomenCount + xymanCount) * 100) + "%");
                        entity.setAgeDistribution(map);
                    } else {
                        entity.setWomanCount(0);
                        entity.setWomanPercent("0%");
                        entity.setManCount(0);
                        entity.setManPercent("0%");
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        map.put("0-20岁", "0.00%");
                        map.put("20-30岁", "0.00%");
                        map.put("30-40岁", "0.00%");
                        map.put("40-50岁", "0.00%");
                        map.put("50-60岁", "0.00%");
                        map.put("60-70岁", "0.00%");
                        map.put("70岁以上", "0.00%");
                        entity.setAgeDistribution(map);
                    }
                }
                flag = false;
                slowDiseaseStatisticsEntities.add(entity);
            }
        }
        return slowDiseaseStatisticsEntities;
    }

    public List<SlowDiseaseStatisticsVo> showTitle(String testKindCode, String year, Long areaCode, String level, String hospId) {
        int allcount = 0 , poctCount = 0 ,xycount = 0;
        int xtcount = 0;
        List<SlowDiseaseStatisticsVo> SlowDiseaseStatisticsVolist = new ArrayList<>();
        List<Long> list = new ArrayList<>();
        if (StringUtils.isBlank(hospId)) {
            List<HlHospitalEntity> hosptitalList = areaGroupService.queryByAreaCode(areaCode, level);
            for (HlHospitalEntity hlHospitalEntity : hosptitalList) {
                list.add(hlHospitalEntity.getId());
            }
        } else {
            list.add(Long.valueOf(hospId));
        }
        //区域测试
        SlowDiseaseStatisticsVo slowDiseaseStatisticsVo1 = new SlowDiseaseStatisticsVo();
        if (list != null && !"".equals(list)) {
            allcount = testprojectitemmonitorDao.queryCountByHospid(list, year);
        }
        slowDiseaseStatisticsVo1.setTestCount(String.valueOf(allcount));
        slowDiseaseStatisticsVo1.setTotalCount(testprojectitemmonitorDao.queryCountByHospid(null, year));
        SlowDiseaseStatisticsVolist.add(slowDiseaseStatisticsVo1);

        //血压测试(由舒张压和收缩压组成)
        SlowDiseaseStatisticsVo slowDiseaseStatisticsVo2 = new SlowDiseaseStatisticsVo();
        if(list != null && !"".equals(list)) {
            allcount = testprojectitemmonitorDao.countTotal("舒张压");
            allcount += testprojectitemmonitorDao.countTotal("收缩压");
            xycount = testprojectitemmonitorDao.countTotalByYear("舒张压", year, list);
            xycount += testprojectitemmonitorDao.countTotalByYear("收缩压", year, list);
        }
        slowDiseaseStatisticsVo2.setTestCount(String.valueOf(xycount));
        slowDiseaseStatisticsVo2.setTotalCount(allcount);
        SlowDiseaseStatisticsVolist.add(slowDiseaseStatisticsVo2);

        //血糖测试
        SlowDiseaseStatisticsVo slowDiseaseStatisticsVo3 = new SlowDiseaseStatisticsVo();
        if(list != null && !"".equals(list)) {
            allcount = testprojectitemmonitorDao.countTotal("空腹血糖");
            allcount += testprojectitemmonitorDao.countTotal("餐后2小时血糖");
            xtcount = testprojectitemmonitorDao.countTotalByYear("空腹血糖", year, list);
            xtcount += testprojectitemmonitorDao.countTotalByYear("餐后2小时血糖", year, list);
        }
        slowDiseaseStatisticsVo3.setTestCount(String.valueOf(xtcount));
        slowDiseaseStatisticsVo3.setTotalCount(allcount);
        SlowDiseaseStatisticsVolist.add(slowDiseaseStatisticsVo3);

        //poct测试
        SlowDiseaseStatisticsVo slowDiseaseStatisticsVo4 = new SlowDiseaseStatisticsVo();
        if (!StringUtils.isBlank(testKindCode)) {
            poctCount = testprojectitemmonitorDao.queryPoctByYear(year,testKindCode,list);
            allcount = testprojectitemmonitorDao.queryPoct(testKindCode);
            slowDiseaseStatisticsVo4.setTestCount(String.valueOf(poctCount));
            slowDiseaseStatisticsVo4.setTotalCount(allcount);
            SlowDiseaseStatisticsVolist.add(slowDiseaseStatisticsVo4);
        }
        return SlowDiseaseStatisticsVolist;
    }

    public List<HlHospitalEntity> showHospital(String testKindCode, String year, Long areaCode, String level) {
        List<HlHospitalEntity> list = areaGroupService.queryByAreaCode(areaCode, level);
        return list;
    }

    public List<SlowDiseaseStatisticsEntity> showMonitor(String testKindCode, String year, String hospId) {
        int allCount = 0, manCount = 0, womenCount = 0, upperCount = 0, lowerCount = 0;        //人数
        int xyallCount = 0, xymanCount = 0, xywomenCount = 0, xyupperCount = 0, xylowerCount = 0;        //人数(血压)
        int totalTime, upperTime, lowerTime;                                            //次数
        int age1 = 0, age2 = 0, age3 = 0, age4 = 0, age5 = 0, age6 = 0, age7 = 0;        //年龄段人数
        int xytotalTime = 0, xyupperTime = 0, xylowerTime = 0;
        int xyage1 = 0, xyage2 = 0, xyage3 = 0, xyage4 = 0, xyage5 = 0, xyage6 = 0, xyage7 = 0;        //年龄段人数(血压)
        boolean flag = true;
        List<SlowDiseaseStatisticsEntity> slowDiseaseStatisticsEntities = new ArrayList<>();
        List<SlowDiseaseStatisticsEntity> list = new ArrayList<>();
        List<TestprojectitemEntity> testprojectitemEntityList = testprojectitemService.queryListMsg(testKindCode);
        for (TestprojectitemEntity testprojectitemEntity : testprojectitemEntityList) {
            SlowDiseaseStatisticsEntity slowDiseaseStatisticsEntity = new SlowDiseaseStatisticsEntity();
            //测试次数固定字段
//            slowDiseaseStatisticsEntity.setTestTotalTitle("测试");
//            slowDiseaseStatisticsEntity.setTestLowerTitle("偏低");
//            slowDiseaseStatisticsEntity.setTestUpperTitle("偏高");
//            //测试人数固定字段
//            slowDiseaseStatisticsEntity.setPersonTotalTitle("测试");
//            slowDiseaseStatisticsEntity.setPersonLowerTitle("偏低");
//            slowDiseaseStatisticsEntity.setPersonUpperTitle("偏高");
            manCount = womenCount = lowerCount = upperCount = 0;
            totalTime = upperTime = lowerTime = 0;
            age1 = age2 = age3 = age4 = age5 = age6 = age7 = 0;

            List<TestprojectitemmonitorEntity> Msg = testprojectitemmonitorDao.queryByYear(
                    testprojectitemEntity.getTestItemCode(), year, hospId);
            allCount = testprojectitemmonitorDao.queryCountByYear(testprojectitemEntity.getTestItemCode(),
                    year);    //测试人数
            for (TestprojectitemmonitorEntity testprojectitemmonitorEntity : Msg) {
                if (testprojectitemmonitorEntity.getType() != null) {
                    if (1 == testprojectitemmonitorEntity.getType()) {
                        upperCount++;        //偏高人数
                    } else if (2 == testprojectitemmonitorEntity.getType()) {
                        lowerCount++;        //偏低人数
                    }
                }
                totalTime += testprojectitemmonitorEntity.getTotalCount();        //测试次数
                lowerTime += testprojectitemmonitorEntity.getLowerCount();        //偏低次数
                upperTime += testprojectitemmonitorEntity.getUpperCount();        //偏高次数

                IdcardInfoExtractorUtil id = new IdcardInfoExtractorUtil(testprojectitemmonitorEntity.getIdcard());
                if ("舒张压".equals(testprojectitemEntity.getTestItemName()) || "收缩压".equals(
                        testprojectitemEntity.getTestItemName())) {
                    if ("0".equals(id.getGender()) || "1".equals(id.getGender())) {
                        xymanCount++;
                    } else if ("2".equals(id.getGender())) {
                        xywomenCount++;
                    }
                    if (id.getAge() >= 0 && id.getAge() < 20) {
                        xyage1++;
                    } else if (id.getAge() >= 30 && id.getAge() < 40) {
                        xyage3++;
                    } else if (id.getAge() >= 40 && id.getAge() < 50) {
                        xyage4++;
                    } else if (id.getAge() >= 50 && id.getAge() < 60) {
                        xyage5++;
                    } else if (id.getAge() >= 60 && id.getAge() < 70) {
                        xyage6++;
                    } else if (id.getAge() >= 20 && id.getAge() < 30) {
                        xyage2++;
                    } else {
                        xyage7++;
                    }
                } else {
                    if ("0".equals(id.getGender()) || "1".equals(id.getGender())) {
                        manCount++;
                    } else if ("2".equals(id.getGender())) {
                        womenCount++;
                    }
                    if (id.getAge() >= 0 && id.getAge() < 20) {
                        age1++;
                    } else if (id.getAge() >= 20 && id.getAge() < 30) {
                        age2++;
                    } else if (id.getAge() >= 30 && id.getAge() < 40) {
                        age3++;
                    } else if (id.getAge() >= 40 && id.getAge() < 50) {
                        age4++;
                    } else if (id.getAge() >= 50 && id.getAge() < 60) {
                        age5++;
                    } else if (id.getAge() >= 60 && id.getAge() < 70) {
                        age6++;
                    } else {
                        age7++;
                    }
                }
            }

            //总标题
            slowDiseaseStatisticsEntity.setPersonTitle(testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setTestTitle(testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setPercentTitle("异常" + testprojectitemEntity.getTestItemName());
            slowDiseaseStatisticsEntity.setAgeDistributionTitle("异常" + testprojectitemEntity.getTestItemName());

            //测试人数相关数据
            slowDiseaseStatisticsEntity.setPersonTotal(String.valueOf(allCount));
            slowDiseaseStatisticsEntity.setPersonUpper(String.valueOf(upperCount));
            slowDiseaseStatisticsEntity.setPersonLower(String.valueOf(lowerCount));

            //测试次数相关数据
            slowDiseaseStatisticsEntity.setTestTotal(String.valueOf(totalTime));
            slowDiseaseStatisticsEntity.setTestUpper(String.valueOf(upperTime));
            slowDiseaseStatisticsEntity.setTestLower(String.valueOf(lowerTime));
            if ((manCount + womenCount) != 0) {
                //异常男女比例
                slowDiseaseStatisticsEntity.setManCount(manCount);
                slowDiseaseStatisticsEntity.setManPercent(
                        new BigDecimal((float) manCount / (manCount + womenCount) * 100).setScale(0,
                                BigDecimal.ROUND_HALF_UP) + "%");
                slowDiseaseStatisticsEntity.setWomanCount(womenCount);
                slowDiseaseStatisticsEntity.setWomanPercent(
                        new BigDecimal((float) womenCount / (manCount + womenCount) * 100).setScale(0,
                                BigDecimal.ROUND_HALF_UP) + "%");

                //异常年龄段分布
                DecimalFormat df = new DecimalFormat("0.00");
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("0-20岁", df.format((float) age1 / (manCount + womenCount) * 100) + "%");
                map.put("20-30岁", df.format((float) age2 / (manCount + womenCount) * 100) + "%");
                map.put("30-40岁", df.format((float) age3 / (manCount + womenCount) * 100) + "%");
                map.put("40-50岁", df.format((float) age4 / (manCount + womenCount) * 100) + "%");
                map.put("50-60岁", df.format((float) age5 / (manCount + womenCount) * 100) + "%");
                map.put("60-70岁", df.format((float) age6 / (manCount + womenCount) * 100) + "%");
                map.put("70岁以上", df.format((float) age7 / (manCount + womenCount) * 100) + "%");
                slowDiseaseStatisticsEntity.setAgeDistribution(map);
            } else {
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                slowDiseaseStatisticsEntity.setManCount(0);
                slowDiseaseStatisticsEntity.setManPercent("0%");
                slowDiseaseStatisticsEntity.setWomanCount(0);
                slowDiseaseStatisticsEntity.setWomanPercent("0%");
                map.put("0-20岁", "0.00%");
                map.put("20-30岁", "0.00%");
                map.put("30-40岁", "0.00%");
                map.put("40-50岁", "0.00%");
                map.put("50-60岁", "0.00%");
                map.put("60-70岁", "0.00%");
                map.put("70岁以上", "0.00%");
                slowDiseaseStatisticsEntity.setAgeDistribution(map);
            }
            if ("舒张压".equals(slowDiseaseStatisticsEntity.getTestTitle()) || "收缩压".equals(
                    slowDiseaseStatisticsEntity.getTestTitle())) {
                list.add(slowDiseaseStatisticsEntity);
            } else {
                slowDiseaseStatisticsEntities.add(slowDiseaseStatisticsEntity);
            }
            if (list.size() == 2 && flag == true) {
                SlowDiseaseStatisticsEntity entity = new SlowDiseaseStatisticsEntity();
//                entity.setPersonTotalTitle("测试");
//                entity.setPersonUpperTitle("偏高");
//                entity.setPersonLowerTitle("偏低");
//                entity.setTestTotalTitle("测试");
//                entity.setTestUpperTitle("偏高");
//                entity.setTestLowerTitle("偏低");
                entity.setTestTitle("血压");
                entity.setPersonTitle("血压");
                entity.setPercentTitle("异常血压");
                entity.setAgeDistributionTitle("异常血压");
                for (SlowDiseaseStatisticsEntity entities : list) {
                    xyallCount += Integer.parseInt(entities.getPersonTotal());
                    xyupperCount += Integer.parseInt(entities.getPersonUpper());
                    xylowerCount += Integer.parseInt(entities.getPersonLower());
                    xytotalTime += Integer.parseInt(entities.getTestTotal());
                    xyupperTime += Integer.parseInt(entities.getTestUpper());
                    xylowerTime += Integer.parseInt(entities.getTestLower());
                    //测试次数
                    entity.setTestTotal(String.valueOf(xytotalTime));
                    entity.setTestUpper(String.valueOf(xyupperTime));
                    entity.setTestLower(String.valueOf(xylowerTime));
                    //测试人数
                    entity.setPersonTotal(String.valueOf(xyallCount));
                    entity.setPersonUpper(String.valueOf(xyupperCount));
                    entity.setPersonLower(String.valueOf(xylowerCount));
                    if ((xywomenCount + xymanCount) != 0) {
                        entity.setWomanCount(xywomenCount);
                        entity.setWomanPercent(
                                new BigDecimal((float) xywomenCount / (xywomenCount + xymanCount) * 100).setScale(0,
                                        BigDecimal.ROUND_HALF_UP) + "%");
                        entity.setManCount(xymanCount);
                        entity.setManPercent(
                                new BigDecimal((float) xymanCount / (xywomenCount + xymanCount) * 100).setScale(0,
                                        BigDecimal.ROUND_HALF_UP) + "%");
                        //异常年龄段分布
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        DecimalFormat df = new DecimalFormat("0.00");
                        map.put("0-20岁", df.format((float) xyage1 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("20-30岁", df.format((float) xyage2 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("30-40岁", df.format((float) xyage3 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("40-50岁", df.format((float) xyage4 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("50-60岁", df.format((float) xyage5 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("60-70岁", df.format((float) xyage6 / (xywomenCount + xymanCount) * 100) + "%");
                        map.put("70岁以上", df.format((float) xyage7 / (xywomenCount + xymanCount) * 100) + "%");
                        entity.setAgeDistribution(map);
                    } else {
                        entity.setWomanCount(0);
                        entity.setWomanPercent("0%");
                        entity.setManCount(0);
                        entity.setManPercent("0%");
                        LinkedHashMap<String, String> map = new LinkedHashMap<>();
                        map.put("0-20岁", "0.00%");
                        map.put("20-30岁", "0.00%");
                        map.put("30-40岁", "0.00%");
                        map.put("40-50岁", "0.00%");
                        map.put("50-60岁", "0.00%");
                        map.put("60-70岁", "0.00%");
                        map.put("70岁以上", "0.00%");
                        entity.setAgeDistribution(map);
                    }
                }
                flag = false;
                slowDiseaseStatisticsEntities.add(entity);
            }
        }
        return slowDiseaseStatisticsEntities;
    }

}
